package com.company.example.task;

import com.company.example.model.pojo.Activity;
import com.company.example.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;

/**
 * Created by ywl on 2017/1/22.
 */
public class ActivitySchedule {
    @Autowired
    private ActivityService activityService;

    // 定时活动线程池
    private static ConcurrentHashMap<Integer,List<ScheduledFuture>>  activitiesMap = new ConcurrentHashMap<Integer,List<ScheduledFuture>>();

    private void init() {
        if(activitiesMap.isEmpty() && activitiesMap.size() == 0) {
            //查询所有需要定时执行的活动计划
            List<Activity> activities = activityService.findUnEndActivities();
            for (Activity activity : activities){
                List<ScheduledFuture> scheduledFutures = activityService.findScheduledList(activity);
                if(!CollectionUtils.isEmpty(scheduledFutures)) {
                    activitiesMap.put(activity.getId(),scheduledFutures);
                }
            }
            System.out.println("<<<<<<<<["+ activitiesMap.size() + "]个活动计划定时任务初始化完成>>>>>>>>");
        }else {
            System.out.println("已经存在需要定时执行的活动计划");
        }
    }

    public static ConcurrentHashMap<Integer, List<ScheduledFuture>> getActivitiesMap() {
        return activitiesMap;
    }
}
